package cn.dgut.training.service.impl;

import cn.dgut.training.dto.activity.CouponListDTO;
import cn.dgut.training.dto.activity.CouponUserDTO;
import cn.dgut.training.entity.activity.Coupon;
import cn.dgut.training.entity.activity.CouponUser;
import cn.dgut.training.mapper.CouponUserMapper;
import cn.dgut.training.service.ICouponUserService;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.activity.CouponVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class CouponUserServiceImpl extends ServiceImpl<CouponUserMapper, CouponUser> implements ICouponUserService {



    @Autowired
    private CouponUserMapper couponUserMapper;

    /**
     * 查询当前优惠券被哪些用户领取
     * @param couponUserDTO
     * @return
     */
    public BaseVO listCouponUserByPage(CouponUserDTO couponUserDTO){

        //构造分页
        IPage page=new Page();
        BeanUtils.copyProperties(couponUserDTO,page);
        //获取条件查询构造器
        QueryWrapper queryWrapper=CouponUserDTO.getWrapper(couponUserDTO);

        IPage res=couponUserMapper.selectPage(page,queryWrapper);

        BaseVO baseVO=new BaseVO(res);

        return baseVO;
    }

    /**
     * 保存优惠券
     * @param userId
     * @param couponId
     */
    public void saveCouponUser(Long userId, Long couponId ) {

        CouponUser couponUser=new CouponUser();
        couponUser.setUserId(userId);
        couponUser.setCouponId(couponId);
        couponUserMapper.insert(couponUser);

    }

    /**
     * 查询当前用户领取编号为couponId的优惠券的数量
     * @param userId
     * @param couponId
     * @return
     */
    public Integer countUserAndCoupon(Long userId, Long couponId) {

        CouponUser c = new CouponUser();
        QueryWrapper<CouponUser> queryWrapper=new QueryWrapper();

        queryWrapper.lambda().eq(CouponUser::getUserId,userId).eq(CouponUser::getCouponId,couponId);

        int count=couponUserMapper.selectCount(queryWrapper);

        return count;

    }


    /**
     * 通过状态分页查询当前用户的优惠券，默认为未使用
     * @param userId
     * @param couponListDTO
     * @return
     */

    public BaseVO<Coupon> selectCurrentCoupons(Long userId,CouponListDTO couponListDTO){


        IPage page=new Page();
        BeanUtils.copyProperties(couponListDTO,page);

        DateFormat fm =new SimpleDateFormat("yyyy-MM-dd");

        List<Coupon> couponList=couponUserMapper.mylist(userId,couponListDTO.getStatus(),page);
        List<CouponVO> res=new ArrayList<>();
        for (Coupon c:couponList) {
            CouponVO couponVO=new CouponVO();
            BeanUtils.copyProperties(c,couponVO);
            res.add(couponVO);
        }

        BaseVO baseVO=new BaseVO(page);
        baseVO.setList(res);

        return baseVO;

    }


}
